﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Components.Services;
using MvcEngine.Mvc.Constraints;
using MvcEngine.Blog.BackOffice.Controllers;
using MvcEngine.Mvc;
using MvcEngine.Core;
using MvcEngine.Blog.Commands;
using MvcEngine.Core.Registry;
using MvcEngine.Blog.ModelBinders;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Filters;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Blog.Services
{
    public class BlogControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_BlogList",
                "BackOffice/Blogs",
                new { controller = "BlogCommon", action = "List" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogInstall",
                "BackOffice/Module/Blog/Install/{pageId}",
                new { controller = "BlogCommon", action = "BlogInstall" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogSettings",
                "BackOffice/Blog/{blogName}/Settings",
                new { controller = "BlogCommon", action = "BlogSettings" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogSettingsSave",
                "BackOffice/Blog/{blogName}/Settings/Save",
                new { controller = "BlogCommon", action = "BlogSettingsSave" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogSettingsImport",
                "BackOffice/Blog/{blogName}/Import",
                new { controller = "BlogCommon", action = "BlogImport" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

        }

        public override  IList<ControlCommand> GetCommands()
        {
            var commands = new List<ControlCommand>();
            commands.Add(new InstallBlogCommand() { Name = "Install", LocalizedName = "Install", Visible = false });
            commands.Add(new BlogListCommand() { Name = "List", LocalizedName = "List", Visible = false });
            return commands;
        }

        public override void RegisterActionFilters(IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCommonController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Blog.BackOffice.Filters.BlogActionFilter));

            //--------------------------------------------------------------------------------------
            //------------------------------End of common filters-----------------------------------
            //--------------------------------------------------------------------------------------

            //BackOffice
            ControllerActionCriteria moduleCriteria = new ControllerActionCriteria();
            moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.List());
            moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.BlogSettings(Guid.Empty));
            moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.BlogSettingsSave(null, null));
            //moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.BlogImport(Guid.Empty, null));
            moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.BlogImport());
            moduleCriteria.AddAction<MvcEngine.Blog.BackOffice.Controllers.BlogCommonController>(s => s.BlogInstall());
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());


            //--------------------------------------------------------------------------------------
            //---------------------------------Permissions------------------------------------------
            //--------------------------------------------------------------------------------------

            ControllerCriteria editCriteria = new ControllerCriteria();
            editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCommonController));
            //--------------------------------------------------------------------------------------
            //---------------------------------End of permissions----------------------------------
            //--------------------------------------------------------------------------------------

            registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(BlogData)] = new BlogModelBinder();
        }

        public override Type GetControllerType()
        {
            return null;
        }

        public override string GetDefaultAction()
        {
            return null;
        }
    }
}
